package com.yilin.tms.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.user.RoleType;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.core.entity.user.organize.UserRole;
import com.yilin.tms.user.service.IGrantRoleMenuService;
import com.yilin.tms.user.service.IRoleService;
import com.yilin.tms.user.mapper.RoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class RoleService extends ServiceImpl<RoleMapper, UserRole> implements IRoleService {

    @Resource
    RoleMapper roleMapper;
    @Resource
    IGrantRoleMenuService grantRoleMenuService;

    /**
     * 获取管理员
     * 初始化 自动创建
     * */
    @Override
    public UserRole initManagerRole(Organize organize) {
        // ### 角色1：对应的管理员，组织管理员默认拥有组织所有API和菜单权限
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organize.getId());
        queryWrapper.eq("role_type", RoleType.manager);
        UserRole managerRole = roleMapper.selectOne(queryWrapper);
        if ( managerRole == null ) {
            managerRole = new UserRole();
            managerRole.setManager(true);
            managerRole.setOrganizeId(organize.getId());
            managerRole.setOrganizeName(organize.getOrganizeName());
            managerRole.setName("组织管理员");
            managerRole.setRoleType(RoleType.manager);
            managerRole.setStatus(true);
            roleMapper.insert(managerRole);
        }
        return managerRole;
    }

    /**
     * 获取组织角色列表1
     * */
    @Override
    public List<UserRole> getUserRoleListByOrganizeId(String organizeId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        return roleMapper.selectList(queryWrapper);
    }
    /**
     * 获取员工角色列表2
     * */
    @Override
    public List<UserRole> getRoleListByOrganizeId(String organizeId,UserRole query) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        QueryUtil.andQueryWrapper(queryWrapper,query);
        queryWrapper.orderByAsc("role_type");
        return roleMapper.selectList(queryWrapper);
    }
    /**
     * 获取指定角色
     * */
    @Override
    public UserRole getUserRoleById(String organizeId, String roleId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        queryWrapper.eq("id", roleId);
        return roleMapper.selectOne(queryWrapper);
    }

    /**
     * 获取管理员角色【一】
     * */
    @Override
    public UserRole getManagerRoleByOrganizeId(String organizeId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        queryWrapper.eq("role_type", RoleType.manager);
        return roleMapper.selectOne(queryWrapper);
    }



    @Override
    public UserRole getEmployeeRoleByName(String organizeId, String name) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        queryWrapper.eq("role_type", RoleType.employee);
        queryWrapper.eq("name", name);
        return roleMapper.selectOne(queryWrapper);
    }


}
